package tasknow.ui;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.wb.swt.SWTResourceManager;

import tasknow.utils.Constants;
import tasknow.utils.Task;

public class ActiveTasksComposite extends Composite {

	// @author A0098740W

	private static Table todaysTable;
	private static Table weeksTable;

	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public ActiveTasksComposite(Composite parent, int style) {
		super(parent, style);
		setFont(SWTResourceManager.getFont("Segoe UI", 11, SWT.NORMAL));
		setBackground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
		setLayout(new GridLayout(1, true));

		Label lblTodaysTasks = new Label(this, SWT.NONE);
		lblTodaysTasks.setFont(SWTResourceManager.getFont("Segoe UI", 11, SWT.BOLD | SWT.ITALIC));
		lblTodaysTasks.setForeground(SWTResourceManager.getColor(SWT.COLOR_YELLOW));
		lblTodaysTasks.setBackground(SWTResourceManager
				.getColor(SWT.COLOR_BLACK));
		lblTodaysTasks.setText("Tasks for Today");

		todaysTable = new Table(this, SWT.BORDER | SWT.FULL_SELECTION);
		todaysTable.setFont(SWTResourceManager.getFont("Segoe UI", 10, SWT.NORMAL));
		todaysTable.setForeground(SWTResourceManager.getColor(SWT.COLOR_YELLOW));
		todaysTable.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
		todaysTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true,
				1, 1));
		todaysTable.setHeaderVisible(true);

		TableColumn tblclmnNumberColumn = new TableColumn(todaysTable,
				SWT.CENTER);
		tblclmnNumberColumn.setResizable(false);
		tblclmnNumberColumn.setWidth(30);
		tblclmnNumberColumn.setText("No");

		TableColumn tblclmnDescriptionColumn = new TableColumn(todaysTable,
				SWT.LEFT);
		tblclmnDescriptionColumn.setWidth(244);
		tblclmnDescriptionColumn.setText("Description");

		Label lblThisWeeksTasks = new Label(this, SWT.NONE);
		lblThisWeeksTasks.setFont(SWTResourceManager.getFont("Segoe UI", 11, SWT.BOLD | SWT.ITALIC));
		lblThisWeeksTasks.setForeground(SWTResourceManager
				.getColor(SWT.COLOR_GREEN));
		lblThisWeeksTasks.setBackground(SWTResourceManager
				.getColor(SWT.COLOR_BLACK));
		lblThisWeeksTasks.setText("Tasks for the Week");

		weeksTable = new Table(this, SWT.BORDER | SWT.FULL_SELECTION);
		weeksTable.setFont(SWTResourceManager.getFont("Segoe UI", 10, SWT.NORMAL));
		weeksTable.setForeground(SWTResourceManager
				.getColor(SWT.COLOR_GREEN));
		weeksTable.setBackground(SWTResourceManager
				.getColor(SWT.COLOR_BLACK));
		weeksTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
				true, 1, 1));
		weeksTable.setHeaderVisible(true);

		TableColumn tblclmnNewColumn_2 = new TableColumn(weeksTable,
				SWT.CENTER);
		tblclmnNewColumn_2.setResizable(false);
		tblclmnNewColumn_2.setWidth(30);
		tblclmnNewColumn_2.setText("No");

		TableColumn tblclmnNewColumn_3 = new TableColumn(weeksTable,
				SWT.LEFT);
		tblclmnNewColumn_3.setWidth(244);
		tblclmnNewColumn_3.setText("Description");
	}

	public static void writeToTodaysTable() {
		todaysTable.removeAll();

		for (int i = 0; i < DisplayAndCommandTextComposite.pullTodaysTasks().size(); i++) {
			TableItem tableItemForToday = new TableItem(todaysTable, SWT.NONE);
			writeToColumn(i, DisplayAndCommandTextComposite.pullTodaysTasks().get(i), tableItemForToday);
		}
	}

	public static void writeToWeeksTable() {
		weeksTable.removeAll();

		for (int i = 0; i < DisplayAndCommandTextComposite.pullNextWeeksTasks().size(); i++) {
			TableItem tableItemForWeek = new TableItem(weeksTable, SWT.NONE);
			writeToColumn(i, DisplayAndCommandTextComposite.pullNextWeeksTasks().get(i), tableItemForWeek);
		}
	}

	private static void writeToColumn(int rowIndex, Task task, TableItem tableItem) {
		tableItem.setText(new String[] {
				Integer.toString(rowIndex + Constants.ADJUST_INDEX_BY_1),
				task.getDescription(), task.getStartDateString(),
				task.getStartTimeString(), task.getEndDateString(),
				task.getEndTimeString(), task.getTaskStatusString() });
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
	
}
